/*
    XTU OJ: 1051
    by: fifth_light
*/

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>

//#define DEBUG_STEP
//#define DEBUG_FIND
//#define DEBUG_MARK
//#define DEBUG_MARK_NUMBER

uint8_t flags_table[1024];
uint16_t jump_left_table[1024], jump_right_table[1024];
int n, m, steps, dir, pos;

void clear_table() {
    memset(flags_table, 0, n);
    memset(jump_left_table, 0, n * 2);
    memset(jump_right_table, 0, n * 2);
    dir = 0;
    pos = 0;
    #ifdef DEBUG_STEP
    steps = 0;
    #endif
}

int fix_pos(int pos) {
    if (pos < 0) {
        return fix_pos(pos + n);
    } else if (pos >= n) {
        return pos % n;
    } else {
        return pos;
    }
}

int find_pos(int pos, int dir) {
    if (dir == 0) {
        pos += jump_right_table[pos] + 1;
    } else {
        pos -= jump_left_table[pos] + 1;
    }
    return fix_pos(pos);
}

void find_next(int dir) {
    #ifdef DEBUG_FIND
    printf("find_next(%d):\n", dir);
    printf("pos: %d\n", pos);
    printf("dir: %d\n", dir);
    printf("jump_left_table: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", jump_left_table[i]);
    }
    printf("\njump_right_table: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", jump_right_table[i]);
    }
    printf("\n");
    #endif
    pos = find_pos(pos, dir);
}

void mark() {
    #ifdef DEBUG_MARK
    assert(flags_table[pos] == 0);
    flags_table[pos] = 1;
    printf("mark():\n");
    #endif
    #ifdef DEBUG_MARK_NUMBER
    printf("pos: %d\n", pos);
    #endif
    #ifdef DEBUG_MARK
    printf("find_pos(pos, 0): %d\n", find_pos(pos, 0));
    printf("find_pos(pos, 1): %d\n", find_pos(pos, 1));
    #endif
    jump_right_table[find_pos(pos, 1)] += jump_right_table[pos] + 1;
    jump_left_table[find_pos(pos, 0)] += jump_left_table[pos] + 1;
    #ifdef DEBUG_MARK
    printf("jump_right_table[find_pos(pos, 0)]: %d\n", jump_right_table[find_pos(pos, 0)]);
    printf("jump_left_table[find_pos(pos, 1)]: %d\n", jump_left_table[find_pos(pos, 1)]);
    #endif
}

int main(void) {
    #ifdef DEBUG_STEP
    int steps = 0;
    #endif
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; i++) {
        scanf("%d%d", &n, &m);
        clear_table();
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < m - 1; k++) {
                #ifdef DEBUG_STEP
                steps++;
                #endif
                find_next(dir);
            }
            mark();
            find_next(dir);
            dir = !dir;
        }
        printf("%d\n", pos + 1);
    }
    #ifdef DEBUG_STEP
    printf("steps: %d\n", steps);
    #endif
    return 0;
}
